//
//  TimePicker.m
//  MicrowaveLight
//
//  Created by Kendama on 2017/6/16.
//  Copyright © 2017年 DT. All rights reserved.
//

#import "TimePicker.h"
@interface TimePicker()

@property(nonatomic,strong)NSMutableArray *hourMutableArray;
@property(nonatomic,strong)NSMutableArray *minuteMutableArray;
@property(nonatomic,copy)NSString *mySelectedHourString;
@property(nonatomic,copy)NSString *mySelectedMinuteString;
@property(nonatomic,strong)NSMutableArray *dataArray;

@end

@implementation TimePicker

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self == [super initWithFrame:frame])
    {
        self.hourMutableArray = [[NSMutableArray alloc]init];
        for (int i = 0; i < 24; i++) {
            [self.hourMutableArray addObject:@(i).stringValue];
        }
        
        self.minuteMutableArray = [[NSMutableArray alloc]init];
        for (int i = 0; i < 60; i++) {
            [self.minuteMutableArray addObject:@(i).stringValue];
        }
        [self.dataArray addObject:self.hourMutableArray];
        [self.dataArray addObject:self.minuteMutableArray];
        
        _pickerView = [[UIPickerView alloc]initWithFrame:self.bounds];
        _pickerView.delegate = self;
        [self addSubview:_pickerView];
        
        UILabel *houLabel = [[UILabel alloc]initWithFrame:CGRectMake(SCREEN_WIDTH/2.0, 216/2-30+1, 30, 60)];
        houLabel.text = @"时";
        houLabel.font = [UIFont systemFontOfSize:12];
        [self addSubview:houLabel];
        
        UILabel *minLabel = [[UILabel alloc]initWithFrame:CGRectMake(SCREEN_WIDTH-20, 216/2-30+1, 30, 60)];
        minLabel.text = @"分";
        minLabel.font = [UIFont systemFontOfSize:12];
        [self addSubview:minLabel];
        
    }
    
    return self;
}


#pragma mark ========================================pikerViewdelegate====================
//一共多少列
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    return self.dataArray.count;
}
//每列对应多少行
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
//    if (component == 0) {
//        return self.hourMutableArray.count;
//    } else if (component == 1) {
//        return self.minuteMutableArray.count;
//    }else
//    {
//        return  0;
//    }
    return [self.dataArray[component]count];

    //    else {
    //        return self.townArray.count;
    //    }
}
//每列每行显示的数据是什么
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
//    if (component == 0) {
//        return [self.hourMutableArray objectAtIndex:row];
//    } else if (component == 1) {
//        return [self.minuteMutableArray objectAtIndex:row];
//    }else{
//        
//        return 0;
//    }
    return self.dataArray[component][row];

}
////组建的宽度
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component {
//    if (component == 0) {
//        return self.frame.size.width/2;
//    } else if (component == 1) {
//        return self.frame.size.width/2;
//    }else{
//        
//        return 0;
//    }
    return SCREEN_WIDTH/(self.dataArray.count + 1);
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    
    
//    if (component==0) {
//        
//        left0_row=row;
//        
//        [_pickerView_left reloadComponent:0];
//        
//    }else{
//        
//        left1_row=row;
//        
//        [_pickerView_left reloadComponent:1];
//        
//    }
//    

    
}

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
    if (!view){
        view = [[UIView alloc]init];
    }
    UILabel *text = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH/3, 50)];
    
    text.font = [UIFont systemFontOfSize:20];
    text.textAlignment = NSTextAlignmentCenter;
    text.text = self.dataArray[component][row];
    text.tag = 10;
    
    [view addSubview:text];
    
    return view;
    
//    UILabel* pickerLabel = (UILabel*)view;
//    if (!pickerLabel){
//        pickerLabel = [[UILabel alloc] init];
//        // Setup label properties - frame, font, colors etc
//        //adjustsFontSizeToFitWidth property to YES
//        pickerLabel.minimumScaleFactor = 8;
//        pickerLabel.adjustsFontSizeToFitWidth = YES;
//        [pickerLabel setTextAlignment:NSTextAlignmentCenter];
//        
//        pickerView.layer.borderWidth=0.5;
//        
//        pickerLabel.tag=row;
//        
//        [pickerLabel setFont:[UIFont systemFontOfSize:15]];
//        
//    }
//    pickerLabel.text=[self pickerView:pickerView titleForRow:row forComponent:component];
//    
//    return pickerLabel;
}

@end
